package com.example.wupic.bestball;

import android.content.Intent;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class LoginActivity extends AppCompatActivity {
    Logger logger = Logger.getLogger("LoginActivity");
    EditText edit_text_account = null;
    EditText edit_text_password = null;
    Button btn_login = null;
    TextView text_register = null;
    TextView text_forgot = null;
    //新建一个请求队列
    RequestQueue queue = MyApplication.requestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
//        queue = Volley.newRequestQueue(this);
        edit_text_account = findViewById(R.id.edit_text_account);
        edit_text_password = findViewById(R.id.edit_text_password);
        btn_login = findViewById(R.id.btn_login);
        text_register = findViewById(R.id.text_register);
        text_forgot = findViewById(R.id.text_forgot);
        text_register = findViewById(R.id.text_register);//注册超链接
        text_forgot = findViewById(R.id.text_forgot);//注册超链接

        text_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开注册页面
                Intent intent = new Intent(getApplicationContext(), RegisterActivity.class);
                startActivity(intent);
            }
        });

        //登录按钮点击事件响应
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开主页MainActivity
                Toast.makeText(getApplicationContext(), "点击", Toast.LENGTH_LONG).show();
                reqlogin();

            }
        });
    }

    public void reqlogin() {

        //1.新建一个volley   json请求
        //获取用户名密码
        String account = edit_text_account.getText().toString();
        String password = edit_text_password.getText().toString();
        String url = "http://www.wupicheng.com:7888/user/login?t_user_account=" + account + "&t_user_password=" + password + "";
        logger.log(Level.WARNING, url);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    Object object = jsonObject.get("success");
                    //把返回的json数据打印出来
                    // Toast.makeText(getApplicationContext(),object.toString(),Toast.LENGTH_LONG).show();
                    logger.log(Level.WARNING, object.toString());
                    if ((object.toString()).equals("true")) {
                        //登录成功
                       JSONObject user= (JSONObject) jsonObject.get("user");
                       MyApplication.session.put("user",user);//登录成功的用户

                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                    } else {
                        //登录失败
                        logger.log(Level.WARNING, "用户名密码有误");
                        Toast.makeText(getApplicationContext(), "用户名密码有误", Toast.LENGTH_LONG).show();

                    }

                } catch (JSONException e) {
                    logger.log(Level.WARNING, e.getMessage());
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        //Set a retry policy in case of SocketTimeout & ConnectionTimeout Exceptions.
//Volley does retry for you if you have specified the policy.
        jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(5000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        queue.add(jsonObjectRequest);//把请求添加到请求队列
//        queue.start();
    }


}
